<?php
class ControllerAccountEdit extends Controller {
	private $error = array();

	public function index() {
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->https('account/edit');

			$this->redirect($this->url->https('account/login'));
		}

		$this->language->load('account/edit');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('account/customer');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_account_customer->editCustomer($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->https('account/account'));
		}

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	); 

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('account/account'),
        	'text'      => $this->language->get('text_account'),
        	'separator' => $this->language->get('text_separator')
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('account/edit'),
        	'text'      => $this->language->get('text_edit'),
        	'separator' => $this->language->get('text_separator')
      	);
		
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_your_details'] = $this->language->get('text_your_details');

		$this->data['entry_firstname'] = $this->language->get('entry_firstname');
		$this->data['entry_lastname'] = $this->language->get('entry_lastname');
		$this->data['entry_email'] = $this->language->get('entry_email');

                // <!-- Start Edit information Ym & Skype By Le_Minh 25/05/2012          -->
                $this->data['entry_ym'] = $this->language->get('entry_ym');
               $this->data['entry_skype'] = $this->language->get('entry_skype');
               
                //<!-- End Edit  information Ym & Skype By Le_Minh 25/05/2012          -->

		$this->data['entry_telephone'] = $this->language->get('entry_telephone');
		$this->data['entry_fax'] = $this->language->get('entry_fax');

		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_back'] = $this->language->get('button_back');

		if (isset($this->error['message'])) {
			$this->data['error'] = $this->error['message'];
		} else {
			$this->data['error'] = '';
		}

		if (isset($this->error['error_firstname'])) {
			$this->data['error_firstname'] = $this->error['error_firstname'];
		} else {
			$this->data['error_firstname'] = '';
		}

		if (isset($this->error['error_lastname'])) {
			$this->data['error_lastname'] = $this->error['error_lastname'];
		} else {
			$this->data['error_lastname'] = '';
		}
		
		if (isset($this->error['error_email'])) {
			$this->data['error_email'] = $this->error['error_email'];
		} else {
			$this->data['error_email'] = '';
		}	
		
		if (isset($this->error['error_telephone'])) {
			$this->data['error_telephone'] = $this->error['error_telephone'];
		} else {
			$this->data['error_telephone'] = '';
		}	

		$this->data['action'] = $this->url->https('account/edit');

		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
			$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
		}

		if (isset($this->request->post['firstname'])) {
			$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($customer_info)) {
			$this->data['firstname'] = $customer_info['firstname'];
		} else {
			$this->data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
			$this->data['lastname'] = $this->request->post['lastname'];
		} elseif (isset($customer_info)) {
			$this->data['lastname'] = $customer_info['lastname'];
		} else {
			$this->data['lastname'] = '';
		}

		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} elseif (isset($customer_info)) {
			$this->data['email'] = $customer_info['email'];
		} else {
			$this->data['email'] = '';
		}

               // <!-- Start Edit information Ym & Skype By Le_Minh 25/05/2012          -->
                if (isset($this->request->post['ym'])) {
			$this->data['ym'] = $this->request->post['ym'];
		} elseif (isset($customer_info)) {
			$this->data['ym'] = $customer_info['ym'];
		} else {
			$this->data['ym'] = '';
		}
                
                if (isset($this->request->post['skype'])) {
			$this->data['skype'] = $this->request->post['skype'];
		} elseif (isset($customer_info)) {
			$this->data['skype'] = $customer_info['skype'];
		} else {
			$this->data['skype'] = '';
		}
                
                
                // <!-- End Edit information Ym & Skype By Le_Minh 25/05/2012          -->






		if (isset($this->request->post['telephone'])) {
			$this->data['telephone'] = $this->request->post['telephone'];
		} elseif (isset($customer_info)) {
			$this->data['telephone'] = $customer_info['telephone'];
		} else {
			$this->data['telephone'] = '';
		}

		if (isset($this->request->post['fax'])) {
			$this->data['fax'] = $this->request->post['fax'];
		} elseif (isset($customer_info)) {
			$this->data['fax'] = $customer_info['fax'];
		} else {
			$this->data['fax'] = '';
		}

		$this->data['back'] = $this->url->https('account/account');
		
		$this->id       = 'content';
		$this->template = $this->config->get('config_template') . 'account/edit.tpl';
		$this->layout   = 'common/layout';
		
		$this->render();		
	}

	private function validate() {
		if ((strlen(utf8_decode($this->request->post['firstname'])) < 3) || (strlen(utf8_decode($this->request->post['firstname'])) > 32)) {
			$this->error['firstname'] = $this->language->get('error_firstname');
		}

		if ((strlen(utf8_decode($this->request->post['lastname'])) < 3) || (strlen(utf8_decode($this->request->post['lastname'])) > 32)) {
			$this->error['lastname'] = $this->language->get('error_lastname');
		}

		$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';

		if ((strlen(utf8_decode($this->request->post['email'])) > 32) || (!preg_match($pattern, $this->request->post['email']))) {
			$this->error['email'] = $this->language->get('error_email');
		}
		
               // <!-- Start Edit information Ym & Skype By Le_Minh 25/05/2012          -->
               if ((strlen(utf8_decode($this->request->post['ym'])) > 32) || (!preg_match($pattern, $this->request->post['ym']))) {
			$this->error['ym'] = $this->language->get('error_email');
                }  
                if ((strlen(utf8_decode($this->request->post['skype'])) > 32) || (!preg_match($pattern, $this->request->post['skype']))) {
			$this->error['skype'] = $this->language->get('error_email');
                 }

                // 
                // 
                //<!-- End Edit  information Ym & Skype By Le_Minh 25/05/2012          -->



		if (($this->customer->getEmail() != $this->request->post['email']) && $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
			$this->error['message'] = $this->language->get('error_exists');
		}

		if ((strlen(utf8_decode($this->request->post['telephone'])) < 3) || (strlen(utf8_decode($this->request->post['telephone'])) > 32)) {
			$this->error['telephone'] = $this->language->get('error_telephone');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>